package com.djsearch.ui;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.djsearch.R;
import com.djsearch.adapter.JingdianAdapter;
import com.djsearch.controller.AsyncHandler;
import com.djsearch.controller.IRequest;
import com.djsearch.controller.IResponse;
import com.djsearch.controller.JSONReq;
import com.djsearch.controller.JSONResp;
import com.djsearch.ui.GroupShopActivity.RequestParamStruct;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 景点
 * 
 * @author yinxing
 * 
 */
public class JingDianActivity extends BaseActivity {
	private Gallery gallery;
	JingdianAdapter adapter;
	RelativeLayout nearByLayout;
	private Button backBtn;
	private TextView chooseCity;
	private List<String> images;
	private TextView searchButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.jingdian_layout);
		// 得到屏幕的分辨率
		getDisPlayMetrics();
		// 初始化主界面
		initView();
		// 初始化主界面相关点击事件
		initOnClick();
		// 初始化handler
		initHandler();
	}

	@Override
	void initHandler() {
		// TODO Auto-generated method stub

	}

	@Override
	void initView() {
		// TODO Auto-generated method stub
		images=new ArrayList<String>();
		nearByLayout = (RelativeLayout) findViewById(R.id.fujin);
		gallery = (Gallery) findViewById(R.id.jingdian_image);
		backBtn = (Button)findViewById(R.id.btnBack);
		chooseCity=(TextView)findViewById(R.id.choose_city);
		searchButton=(TextView)findViewById(R.id.search_Button);
		requestImageData();
		adapter = new JingdianAdapter(this,images);
		gallery.setAdapter(adapter);
	}

	@Override
	void initOnClick() {
		// TODO Auto-generated method stub

		nearByLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(JingDianActivity.this, NearbyActivity.class, 0);
			}
		});
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

//				startActivity(JingDianActivity.this, MainActivity.class, 1);
				finish();
			}
		});
		chooseCity.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(JingDianActivity.this, ChooseCityActivity.class,
						0);
			}
		});
		searchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				requestSearchData();
			}
		});
	}

	@Override
	void getDataToResolve(JSONResp responseJSON) {
		// TODO Auto-generated method stub
		try {
			Gson gson = new Gson();
			Type type = new TypeToken<List<String>>() {
			}.getType(); // 指定集合对象属性
			List<String> fromJson = gson.fromJson(
					responseJSON.getString("returnMessage"), type);
			images.addAll(fromJson);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 请求景点首页大图
	 */
	private void requestImageData() {
		IRequest request = new JSONReq("jingdian_picture");

		HTTP.post(request, new AsyncHandler() {
			@Override
			public void onStart() {
				// showProgressBar(backBtn);
				showDialog("正在加载...");
			}

			@Override
			public void onSuccess(JSONResp response) {
				// TODO Auto-generated method stub
				int result = response.result();
				if (IResponse.Success == result) {
					getDataToResolve(response);
					Toast.makeText(JingDianActivity.this, "成功", 5000).show();
				} else if (IResponse.Fail == result) {
					// 返回数据失败
					getDataToResolve(response);
					Toast.makeText(JingDianActivity.this, "无数据", 5000).show();
				}
				missDialog();
			}

			@Override
			public void onFailure(Throwable e, String response) {
				// TODO Auto-generated method stub
				missDialog();
//				Toast.makeText(JingDianActivity.this, response, 5000).show();
			}

		});
	}

	/**
	 * 搜索景点
	 */
	private void requestSearchData() {
		IRequest request = new JSONReq("jingdian_search");

		HTTP.post(request, new AsyncHandler() {
			@Override
			public void onStart() {
				// showProgressBar(backBtn);
				showDialog("正在加载...");
			}

			@Override
			public void onSuccess(JSONResp response) {
				// TODO Auto-generated method stub
				int result = response.result();
				if (IResponse.Success == result) {
					getDataToResolve(response);
					Toast.makeText(JingDianActivity.this, "成功", 5000).show();
				} else if (IResponse.Fail == result) {
					// 返回数据失败
					getDataToResolve(response);
					Toast.makeText(JingDianActivity.this, "失败", 5000).show();
				}
				missDialog();
			}

			@Override
			public void onFailure(Throwable e, String response) {
				// TODO Auto-generated method stub
				missDialog();
				Toast.makeText(JingDianActivity.this, response, 5000).show();
			}

		});
	}

	/**
	 * 点击返回键
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(JingDianActivity.this, MainActivity.class, 1);
		}
		return false;
	}
	
}
